import { getCookie, setCookie, removeCookie } from '@utils/jsCookie.js'
import { NEWHISTORY } from '@/constant'
import { defineStore } from 'pinia'
export const useHistoryStore = defineStore('history', {
  state: () => ({
    history: getCookie(NEWHISTORY) || {}
  }),

  actions: {
    async unshiftHistory(data) {
      let now = new Date()
      let year = now.getFullYear()
      let month = now.getMonth() + 1
      let date = now.getDate()
      let nowDate = `${year}-${month}-${date}`
      let historyData = getCookie(NEWHISTORY)
      historyData = historyData || {}

      if (historyData[nowDate]) {
        historyData[nowDate].unshift(data)
      } else {
        historyData[nowDate] = []
        historyData[nowDate].unshift(data)
      }
      setCookie(NEWHISTORY, historyData)
      this.history = historyData
    },
    async delHistory() {
      removeCookie(NEWHISTORY)
      this.history = []
    }
  }
})
